package com.cuiweiyou.bluemesh.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.cuiweiyou.bluemesh.R;
import com.cuiweiyou.bluemesh.bluetooth.BluetoothMaster;
import com.cuiweiyou.bluemesh.bluetooth.LightMaster;
import com.cuiweiyou.bluemesh.model.EventDeviceModel;
import com.cuiweiyou.bluemesh.model.EventLightModel;
import com.cuiweiyou.bluemesh.model.LightModel;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class GroupCommFragment extends Fragment {

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    private String groupName;
    private List<LightModel> lightModelList;

    private DevicesCommAdapter devicesCommAdapter;
    private OnBluetoothProcessingListener onBluetoothProcessingListener;

    public static GroupCommFragment newInstance(String group, List<LightModel> model, OnBluetoothProcessingListener listener) {

        ArrayList<LightModel> lightList = new ArrayList<>();
        lightList.addAll(model);

        Bundle bundle = new Bundle();
        bundle.putString("groupName", group);
        bundle.putSerializable("lightList", lightList);
        bundle.putSerializable("listener", listener);

        GroupCommFragment fragment = new GroupCommFragment();
        fragment.setArguments(bundle);

        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Bundle bundle = getArguments();
        groupName = bundle.getString("groupName");
        lightModelList = (List<LightModel>) bundle.getSerializable("lightList");
        onBluetoothProcessingListener = (OnBluetoothProcessingListener) bundle.getSerializable("listener");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        EventBus.getDefault().register(this);
        View view = inflater.inflate(R.layout.fragment_item_commgroup, container, false);
        ButterKnife.bind(this, view);

        initView();

        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }

    @OnClick(R.id.openAllButton)
    public void onOpenAllButtonClicked() {
        processAllLight(true);
    }

    @OnClick(R.id.closeAllButton)
    public void onCloseAllButtonClicked() {
        processAllLight(false);
    }

    private void processAllLight(boolean isAllOpen) {
        if (null != onBluetoothProcessingListener) {
            onBluetoothProcessingListener.OnBluetoothProcessingStart();
        }
        LightMaster.getInstance().openOrCloseAllLight(groupName, isAllOpen);
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onDeviceEvent(EventDeviceModel model) { // 收到设备列表刷新
        flushDeviceList();  // 收到设备列表刷新
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onLightEvent(EventLightModel model) { // 开关灯状态刷新
        if (groupName.equalsIgnoreCase(model.getGroup())) {
            if (0 == model.getFlag()) {
                Toast.makeText(getActivity(), "没有设备", Toast.LENGTH_SHORT).show();
            } else if (-1 == model.getFlag()) {
                Toast.makeText(getActivity(), "没有设备在线", Toast.LENGTH_SHORT).show();
            } else {
                flushDeviceList(); // 开关灯状态刷新
            }

            if (null != onBluetoothProcessingListener) {
                onBluetoothProcessingListener.OnBluetoothProcessingFinish();
            }
        }
    }

    private void initView() {
        devicesCommAdapter = new DevicesCommAdapter();
        devicesCommAdapter.setOnDeviceSelectedListener(onDeviceSelectedListener);

        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);

        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(devicesCommAdapter);

        devicesCommAdapter.updateDevices(lightModelList);
    }

    // 点击了某个设备
    private DevicesCommAdapter.OnDeviceSelectedListener onDeviceSelectedListener = new DevicesCommAdapter.OnDeviceSelectedListener() {
        @Override
        public void onDeviceSelected(LightModel model) {
            if (model.isOnline()) {
                if (null != onBluetoothProcessingListener) {
                    onBluetoothProcessingListener.OnBluetoothProcessingStart();
                }
                LightMaster.getInstance().flushLight(model); // 点击了某个设备
            } else {
                Toast.makeText(getActivity(), "设备不在线", Toast.LENGTH_SHORT).show();
            }
        }
    };

    private void flushDeviceList() {
        List<LightModel> deviceResultList = BluetoothMaster.getInstance().getDeviceListByGroup(groupName);
        if (null != devicesCommAdapter) {
            devicesCommAdapter.updateDevices(deviceResultList);
        }
    }
}
